#!/usr/bin/env python
# -*- coding: utf-8 -*-
from gameinit import *
from board import *

LEFT = 1
RIGHT = 2

class Ship(board):
    def __init__(self,filename,players,owner=None):
        self.centre = None
        board.__init__(self,filename,players)
        
        self.pos = 0
        self.owner = owner

    def load(self,filename):
        board.load(self,filename)
        self.side = self.width/2
        self.centre = (self.width/2,self.height/2)
    
    def new(self,size):
        self.centre= (self.width/2,self.height/2) 
        w,h = size
        if w % 2 == 0:
            print "WARNING : ship maps should have an odd width/height"
            w+=1
        if h % 2 == 0:
            h+=1
            print "WARNING : ship maps should have an odd width/height"
                
        if w != h:
            print "WARNING : ship maps should have width equal to height"
            w = max(w,h)
            w = h
        self.size = (w,h)
        board.new(self,self.size)
        self.centre = (w/2,h/2)
        side = (h - 1) / 2
        self.side = side
        cx,cy = self.centre
        
        for h in self.hexs:
            h.setOutOfBoard()
   
 
        self.getHex(cx,cy).unsetOutOfBoard()
            
        for i in range(1,side+1):
            hexs = self.getCouronne(i)
            for h in hexs:
                h.unsetOutOfBoard()
        
    def __rotateList(self,l,direction=LEFT):
        n = len(l)
        res = []
        if direction == RIGHT :
            step = n / 6
        else :
            step = -n / 6
            
        for i in range(n):
            res.append(l[(i+step+n) % n])
        return res

    def rotate(self,direction = LEFT):
        posincr = 0
        if direction == LEFT:
            posincr =1
        else:
            posincr = -1

        self.pos = (self.pos + posincr + 6) % 6
        
        for i in range(1,self.side+1):
            oldCoords = []
            hexs = self.getCouronne(i)
            newHexs = []
            for h in hexs:
                oldCoords.append(h.getCoord())
                newHexs.append(h.clone())
            
            newCoords = self.__rotateList(oldCoords,direction)
            for i in range(len(newHexs)):
                newHexs[i].setCoord(newCoords[i])
                newHexs[i].addPos(posincr)
                self.setHex(newHexs[i])
    
    def turnLeft(self):
        self.rotate(LEFT)
            

    def turnRight(self):
        self.rotate(RIGHT)
       
    def getCouronne(self,n):
        #couronne = []
        hexs = []
        pos = SE
        x0,y0 = self.centre
        hexDepart = self.getHex(x0,y0-n)
        currHex = hexDepart
        for i in range(6):
            for j in range(n):
                currHex=self.getNeighbourHexPos(currHex.getCoord(),pos)
                #couronne.append(currHex.getCoord())
                hexs.append(currHex)
                
            pos = pos - 1
            if pos < 0:
                pos = 5

        return hexs


        
if __name__ == "__main__":
    import pygl2d
    import gui
    from player import *
    from gamewidgets import *    

    desktop = gui.Desktop()
    player = Player("player1") 
    players = [player]
    
    ship = Ship("data/shipmaps/weird.txt",players,player)
    ship.setCurrentPlayer(player)
    for k in range(1,8):
        couronne =  ship.getCouronne(k)
        for i in range(len(couronne)):
            h=couronne[i]
            h.setText("%d" % i)
            h.highlight(player)
    

    windowMap=WindowMap(ship,position = (0,0),size=(800,SCREEN_HEIGHT), parent = desktop)
    winHexSize=windowMap.getWindowHexSize()
    minimapship = Minimap(ship,position = (SCREEN_WIDTH-210,100),size=(210,210),winHexSize=winHexSize, parent = desktop)
    hexDetails = HexDetails(player,position=(SCREEN_WIDTH-210,0),size=(210,100),parent = desktop)
    minimapship.onClick = miniMapOnClick
    windowMap.onClick = WindowMapOnClick

    
    
    print pygame.display.get_surface().get_size()
    print windowMap.getWindowHexSize()
    
    clock = pygame.time.Clock()
    running = 1
    pygame.display.flip()

    while running:
        clock.tick(60.0)

        for e in gui.setEvents():
            if e.type == QUIT:
                running = 0
            if e.type == KEYDOWN:
                if e.key == K_ESCAPE:
                    running = 0
                if e.key == K_LEFT:
                    ship.turnLeft()
                if e.key == K_RIGHT:
                    ship.turnRight()


        desktop.update()

        x0,y0,x1,y1 = minimapship.getWindowHex()

        windowMap.updateCoords(x0,y0)

        pygl2d.window.begin_draw()
        
        
        desktop.draw()
        
        pygl2d.window.end_draw()
        pygame.display.flip()
        display.set_caption(str(int(clock.get_fps())))
            
        

    pygame.quit()        
            
      
        
